/**
 * @Title: AppContext.java
 * @Package: com.jason.bluetooth.fingerprint
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月18日 下午5:19:48
 * @Version: V1.0
 */
package com.jason.platform.supervision;

import android.app.Application;
import android.content.Context;
import com.baidu.mapapi.SDKInitializer;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.UserInfoBean;
import com.jason.platform.supervision.log.Log;
import com.jason.platform.supervision.task.HttpConfig;
import com.jason.platform.supervision.utils.CommonUtils;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.tencent.bugly.crashreport.CrashReport;
import org.kymjs.aframe.utils.SystemTool;

/**
 * @ClassName: AppContext
 * @Description: 全局应用程序类：用于保存和调用全局应用配置及访问网络数据
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月18日 下午5:19:48
 */
public class BootApp extends Application {
	
	private static final String TAG = "BootApp";

	private static final boolean DB_ISDEBUG = true;
	private static final String DB_NAME = "SupervisionPlatform";
	private static final int DB_VERSION = 1;
	public static final int PAGE_SIZE = 20;// 默认分页大小
	public static final boolean RECORD_SHOW_DIALOG = true;

	private static final String BUGLY_APPID = "900001968";
	private static final boolean BUGLY_DEBUG = true ;  //true代表App处于调试阶段，false代表App发布阶段

	public static final boolean NEW_VERSION_FLAG = false;//是否是第二次版本

	/** 单例*/
	public static BootApp mInstance;
	/** 全局Context*/
	public static Context mAppContext;

	//登录者用户信息
	public static UserInfoBean USER_INFO = null;

	public static Integer USER_ORANGE_ID = 0;

	public static BootApp getInstance(){
		return mInstance;
	}
	
	public static Context getAppContext(){
		if (mAppContext == null)
			mAppContext = mInstance.getApplicationContext();
		return mAppContext;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		mAppContext = getApplicationContext();
		//TODO 初始化日志
		initCrashReport();
		//初始化百度地图
		SDKInitializer.initialize(mAppContext);
		//程序异常崩溃捕获
//		initLog();
//		Thread.setDefaultUncaughtExceptionHandler(new UncaughtException());
//		GAUtils.initGA(mAppContext, Constant.GA_TRACK_ID);
		initImageLoader(mAppContext);
		//初始化当前ip,端口
		HttpConfig.setHost(SharedPreferencesUtils.getHttpHost(mAppContext));
		HttpConfig.setPort(SharedPreferencesUtils.getHttpPort(mAppContext));
	}

	public void initCrashReport(){
		CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(mAppContext);
		userStrategy.setAppVersion(getResources().getString(R.string.app_name) + "_v" + CommonUtils.getVersionName(mAppContext));
		userStrategy.setAppChannel("慧智灵杰");
		userStrategy.setDeviceID(getImei());
		userStrategy.setAppReportDelay(10l);
		CrashReport.initCrashReport(mAppContext, "900007987", false, userStrategy);
		CrashReport.setUserId("慧智灵杰" + getImei());
	}

	public void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.MIN_PRIORITY)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.discCacheSize(20 * 1024 * 1024)
				.memoryCacheSizePercentage(20)
				.tasksProcessingOrder(QueueProcessingType.LIFO)
//				.discCache(new UnlimitedDiskCache(StorageUtils.getOwnCacheDirectory(context, Constant.BASE_FOLDER + "/img-cache")))
				.threadPoolSize(5)
				.build();
		ImageLoader.getInstance().init(config);
	}

	/**
	 * 初始化日志配置
	 */
	public void initLog(){
		Log.setDebug(true);
		Log.setPrint(true);
		Log.setPrintFile(true);
		Log.setMaxLogFileLength(10*1024*1024);
		Log.setLogFilePath(Constant.getCacheRootPath()+"/log");
		Log.i(TAG, "log init success");
	}

	/**
	 * 当程序出现未捕获异常崩溃时调用
	 */
	public void onFC(){
		Log.e(TAG, "onFC");
		int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);
	}

	/**
	 * @return the imei 当前设备的imei号码
	 */
	public String getImei() {
		return SystemTool.getPhoneIMEI(this);
	}

}
